<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:bm="livcos.org/ns/bm"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="doc bm">
  
  <xsl:template match="bm:competition">
    <div class="table">
      <table border="1" cellpadding="4" cellspacing="0">
        <thead>
          <tr>
            <th>Aspect</th>
            <xsl:apply-templates select="bm:category/bm:competitor" mode="header"/>
          </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="bm:aspect" />
        </tbody>
      </table>
    </div>
  </xsl:template>
  
  <xsl:template match="bm:aspect">
    <tr id="{@id}">
      <td>
        <xsl:value-of select="bm:title" />
      </td>
      <xsl:apply-templates select="../bm:category/bm:competitor">
        <xsl:with-param name="aspect" select="."/>
      </xsl:apply-templates>
    </tr>
  </xsl:template>
  
  <xsl:template match="bm:competitor">
    <xsl:param name="aspect"/>
    <xsl:variable name="rating" select="bm:rating[@aspect = $aspect/@id]"/>
    <xsl:choose>
      <xsl:when test="$rating">
        <td align="center" id="{$rating/@id}">
          <xsl:value-of select="$rating/@mark" />
        </td>
      </xsl:when>
      <xsl:otherwise>
        <td align="center">-</td>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="bm:competitor" mode="header">
    <th><xsl:value-of select="@code"/></th>
  </xsl:template>
  
</xsl:stylesheet>
